package com.gzc.just.play.last.war.robotclient.handler;

import com.gzc.just.play.last.war.common.annotation.MessageHandler;
import com.gzc.just.play.last.war.common.handler.SimpleMessageHandler;
import com.gzc.just.play.last.war.robotclient.NettyRobotClient;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@MessageHandler(serverTypes = MessageHandler.ServerType.ROBOT_CLIENT)
public class HeartbeatResponseHandler implements SimpleMessageHandler {
    private static final Logger log = LoggerFactory.getLogger(HeartbeatResponseHandler.class);

    @Override
    public void handle(Object message, ChannelHandlerContext ctx) throws Exception {
        NettyRobotClient client = ctx.channel().attr(NettyRobotClient.CLIENT_KEY).get();
        if (client == null) {
            log.warn("HeartbeatResponseHandler: Robot client not found in channel attributes");
            return;
        }
        
        // 更新客户端状态
        client.onHeartbeatReceived();
        log.debug("Robot {} received heartbeat response", client.getRobotId());
    }

    @Override
    public int getMessageId() {
        return 1002;
    }

    @Override
    public String getName() {
        return "HeartbeatResponseHandler";
    }

    @Override
    public Logger getLogger() {
        return log;
    }
}
